package com.xbongbong.saas.service.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.help.subform.TransferSubFormHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.refunduser.pojo.dto.RefundUserDeleteBatchDTO;
import com.xbongbong.pro.refunduser.pojo.vo.RefundUserDeleteBatchVO;
import com.xbongbong.pro.service.toolbox.elasticsearch.help.ProEsHelper;
import com.xbongbong.saas.constant.ParameterConstant;
import com.xbongbong.saas.domain.entity.RefundUserEntity;
import com.xbongbong.saas.model.RefundUserModel;
import com.xbongbong.saas.service.RefundUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author haibin.zhang
 * @version v1.0
 * @date 2019/4/11 14:59
 * @since v1.0
 */
@Service("refundUserService")
public class RefundUserServiceImpl implements RefundUserService {

    private static final Logger LOG = LoggerFactory.getLogger(RefundUserServiceImpl.class);

    @Resource
    private RefundUserModel refundUserModel;
    @Resource
    private ProEsHelper proEsHelper;
    @Resource
    private TransferSubFormHelper transferSubFormHelper;

    @Override
    public RefundUserDeleteBatchVO deleteBatch(RefundUserDeleteBatchDTO refundUserDeleteBatchDTO) throws XbbException {
        RefundUserDeleteBatchVO refundUserDeleteBatchVO = new RefundUserDeleteBatchVO();
        try {
            List<Long> refundIdIn = refundUserDeleteBatchDTO.getRefundIdIn();
            String corpid = refundUserDeleteBatchDTO.getCorpid();
            refundUserModel.deleteBatchByRefundIdIn(refundIdIn, corpid);
        } catch (Exception e) {
            LOG.error("RefundUserServiceImpl.deleteBatch 执行出错：", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
        return refundUserDeleteBatchVO;
    }

    /**
     * 更新回滚es
     *
     * @param refundIdIn
     * @param corpid
     * @throws XbbException
     */
    @Override
    public void updateRollbackByRefundId(List<Long> refundIdIn, String corpid) throws XbbException {
        if (Objects.isNull(refundIdIn) || refundIdIn.isEmpty()) {
            return;
        }
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put(ParameterConstant.CORPID, corpid);
        param.put("refundIdIn", refundIdIn);
        List<RefundUserEntity> performanceEntityList = refundUserModel.findEntitys(param);
        if (Objects.nonNull(performanceEntityList)) {
            List<PaasFormDataEntity> paasFormDataEntities = transferSubFormHelper.transferUserTeamToFormDataList(IndexTypeEnum.IDX_SAAS_REFUND_TEAM, performanceEntityList);
            for (PaasFormDataEntity paasFormDataEntity : paasFormDataEntities) {
                proEsHelper.reductionEsInUpdate(paasFormDataEntity, paasFormDataEntity.getId(), corpid, IndexTypeEnum.IDX_SAAS_REFUND_TEAM);
            }
        }
    }
}
